//
//  ViewController.m
//  20--XML
//
//  Created by 冀永金 on 16/8/10.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic,strong) NSMutableArray *stuArray;
@property (nonatomic, copy) NSString *currentElementName;
@property (nonatomic, strong) NSMutableArray *domMarray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//  XML 分为两种解析方式
// 第一种SAX逐行解析  ----使用系统的解析器 NSXMLParser
- (IBAction)saxParse:(id)sender {
    NSString *xmlPath = [[NSBundle mainBundle]pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmlParaer = [[NSXMLParser alloc]initWithData:data];
    xmlParaer.delegate = self;
    [xmlParaer parse];
}
// 解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
// 遇到结点头 调用的方法 记录当前节点 继续方便赋值
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [[Student alloc]init];
        [_stuArray addObject:stu];
    }
}
// 节点值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    // string 是\n\t
//    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
    if (_currentElementName) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
        NSLog(@"%@",string);
        
    }
//    }
    
}

// 遇到结点尾 方法回调
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    _currentElementName = nil;
}
// 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@",_stuArray);
}
// 第二种 解析方式 DOM解析
// 关于GDataXMLNode 的引入
// 引入.h与.m文件
// 引入系统动态库libxml2.tbd
// 3.根据GDataXMLNode头文件中的提示，添加/user/include/libxml2
// 4.如果是ARC环境，需对该文件进行编译，添加 -fno-objc-arc
- (IBAction)domParse:(id)sender {
    self.domMarray = [NSMutableArray array];
    NSString *xmlDataPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:xmlDataPath];
    NSError *error = nil;
    // 创建GDataXMLDocument 对象
    // 根据XML 的Data数据生成一个树形结构的 Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithData:data options:0 error:&error];
    // 根据Documnet 获取跟节点
    GDataXMLElement *rootElement = [document rootElement];
    // 根据节点获取子节点数组
    NSArray *rootChildElement = rootElement.children;
    for (GDataXMLElement *childElemnet in rootChildElement) {
        // 对应所有sutent的节点
        Student *stu = [Student new];
        [self.domMarray addObject:stu];
        // 便利stuent节点的所有子节点
        for (GDataXMLElement *grandSonElement in childElemnet.children) {
            // 赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
            
        }
    }
    NSLog(@"%@",self.domMarray);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
